import Global from "@/Global";
import { checkPersonLogin, getPersonID, hasResume } from "@/util/session";
import { PersonLoginedGetAppResumeMain } from "@/util/http";
import { returnToPhoneWithData } from "@/util/phone";
import { Toast } from "vant";
import encrypt from "@/util/encrypt";
import chatHub from "@/util/chatHub";
//个人
export const mixin_person = {
	methods: {
		login(data,call) {
			this.$api.post(this.$api.pActions.Login, data, res => {
				if (res.success) {
					//save data for this login
					var personInfo = res.response;
					this.loginSuccess(personInfo);
					if(call){call()}
				}
			})
		},
		loginSuccess(personInfo){
			addPersonLoginData(personInfo.personID, personInfo.token, personInfo.mobileNum, personInfo.resumeID);
			this.$bus.$emit(Global.bus_person_login,true);
			//login to jiguang
			returnToPhoneWithData({action:'person_im_login',personid:personInfo.personID})
		}
	}
}
export const mixin_all = {
	methods:{
		toCall(tel){
			returnToPhoneWithData({
				action: "call",
				tel,
			});
		}
	}
}
//手机触发返回
export const mixin_back = {
	created() {
		window.onBack = this.onBack;
	},
	// 方法集合
	methods: {
		onBack() {
			this.$router.go(-1);
		},
		onClickLeft() {
			this.$router.go(-1);
		},
	},
};

//打电话
export const mixin_call = {
	methods: {
		toCall(tel) {
			returnToPhoneWithData({
				action: "call",
				tel,
			});
		},
	},
};

//个人-跳转职位、企业
export const mixin_person_toJobCompany = {
	// 方法集合
	methods: {
		onJobClick(jobid) {
			this.$router.push({ name: "person-jobinfo", query: { jobid } });
		},
		onCompanyClick(companyid) {
			this.$router.push({ name: "person-companyinfo", query: { companyid } });
		},
	},
};

//个人-提问列表
export const mixin_person_toAsk = {
	// 方法集合
	methods: {
		onViewAskClick(list) {
			// console.log("onViewAskClick", list);
			// console.log(JSON.stringify(list));
			this.$router.push({
				name: "person-asklist",
				params: { list },
			});
		},
	},
};

//个人-去登录
export const mixin_person_tologin = {
	methods: {
		onToLogin() {
			this.$router.push({ name: "person-login" });
		},
	},
};
//个人-去创建简历
export const mixin_person_toCreateResume = {
	methods: {
		onToCreateResume() {
			this.$router.push({ name: "person-resume-resumequick1" });
		},
		onToCreateResumeReplace() {
			this.$router.replace({ name: "person-resume-resumequick1" });
		},
	},
};

//个人-简历-完成简历提交后返回
export const mixin_person_resumeSumbitBack = {
	methods: {
		goBack() {
			this.$bus.$emit(Global.bus_person_resume_update, "ok");
			setTimeout(() => {
				this.$router.go(-1);
			}, Global.setTimeout_time);
		},
	},
};

//个人-找企业聊一聊
export const mixin_person_toIM = {
	methods: {
		onIM(companyid) {
			if (!checkPersonLogin()) {
				//没登录 去登录
				this.$router.push({ name: "person-login" });
				return;
			}
			//判断有无简历
			if (hasResume()) {
				//创建聊天会话
				this.$api.post('/PersonIM/CreateConversationByPerson?companyId=' + companyid).then(res => {
					if (res.success) {
						this.$router.push({
							name: "person-im",
							query: { cidEncrypt: encrypt.AESencrypt(res.response), companyIDEncrypt: encrypt.AESencrypt(companyid) },
						});
					}
					else {
						Toast.fail('创建聊天失败');
					}
				})
			}
			else {
				this.$router.push({ name: "person-resume-resumequick1" });
			}
		},
	},
};

//微信
export const mixin_wx = {
	methods: {
		//调起微信小程序-企业
		toWXXCXCompany() {
			returnToPhoneWithData({
				action: "open_wx_xcx",
				wxid: "gh_a5f802985beb",
			});
		},
		//微信客服个人
		toWXKFPerson() {
			returnToPhoneWithData({
				action: "open_wx_kf",
				url: "https://work.weixin.qq.com/kfid/kfc85cbd93983125ac7",
			});
		},
		toWXKF(url) {
			returnToPhoneWithData({
				action: "open_wx_kf",
				url,
			});
		},
	},
};

//正则验证
export const mixin_validate = {
	data() {
		return {
			validate_phone: /\d{11}/,
			validate_code: /\d{6}/,
			validate_phoneOrTel:
				/^(\d{11})$|^((\d{3}-)?\d{8}(-\d{1,4})?)$|^((\d{4}-)?\d{7,8}(-\d{1,4})?)$|^(\d{7,8})$/,
			validate_email: /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/,
		};
	},
};

//企业
export const mixin_company_common = {
	methods: {
		toMyKF() {
			this.$router.push({ name: "company-kf" });
		},
		toViewResume(resumeid, rowid) {
			// console.log("看简历", resumeid, rowid);
			this.$router.push({
				name: "company-resume-resumeinfo",
				query: { resumeid, rowid },
			});
		},
		toViewResume2(resumeid, rowid, isFromIM) {
			// console.log("看简历", resumeid, rowid);
			this.$router.push({
				name: "company-resume-resumeinfo",
				query: { resumeid, rowid, isFromIM },
			});
		},
	},
};

export const mixin_common = {
	methods: {
		toIMReport(reportedID, reportUserType, name) {
			// this.$router.push({
			// 	name: "company-im-report",
			// 	query: { reportedID, reportUserType, name },
			// });
			Toast('举报成功')
		},
	},
};
